package com.smsc.headend.task.engine.service.collection.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.SystemClock;
import cn.hutool.core.lang.Pair;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smsc.headend.common.constant.CacheNameConstant;
import com.smsc.headend.module.collection.dto.CaptureObject;
import com.smsc.headend.module.meterscheme.entity.CaptureObjsStatus;
import com.smsc.headend.task.engine.mapper.CaptureObjsStatusMapper;
import com.smsc.headend.task.engine.service.collection.CaptureObjsStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaosc
 * @date 2021/3/25 11:01
 */
@Service
@Slf4j
@CacheConfig(cacheNames = CacheNameConstant.CAPTURE_OBJECTS)
public class CaptureObjsStatusServiceImpl extends ServiceImpl<CaptureObjsStatusMapper, CaptureObjsStatus> implements CaptureObjsStatusService {

    @Override
    @CacheEvict(key = "#meterId + '.' + #lpTemplateId")
    public void saveCaptureObjs(Long meterId, Long lpTemplateId, JSONArray result, JSONArray failResult, int status) {
        if (CaptureObjsStatus.Status.Success.getCode() == status) {
            LambdaQueryWrapper<CaptureObjsStatus> queryWrapper = Wrappers.<CaptureObjsStatus>lambdaQuery().eq(CaptureObjsStatus::getMeterId, meterId)
                    .eq(CaptureObjsStatus::getLpTemplateId, lpTemplateId);
            int count = count(queryWrapper);
            if (count == 0) {
                CaptureObjsStatus objsStatus = CaptureObjsStatus.builder().meterId(meterId).lpTemplateId(lpTemplateId).status(status).result(JSONUtil.toJsonStr(result)).failResult(JSONUtil.toJsonStr(failResult)).build();
                save(objsStatus);
                return;
            }
        }

        LambdaUpdateWrapper<CaptureObjsStatus> updateWrapper = Wrappers.<CaptureObjsStatus>lambdaUpdate().eq(CaptureObjsStatus::getMeterId, meterId)
                .eq(CaptureObjsStatus::getLpTemplateId, lpTemplateId).set(CaptureObjsStatus::getTvUpdate, SystemClock.now() / 1000)
                .set(CaptureObjsStatus::getStatus, status).set(result != null, CaptureObjsStatus::getResult, JSONUtil.toJsonStr(result))
                .set(failResult != null, CaptureObjsStatus::getFailResult, JSONUtil.toJsonStr(failResult));
        update(updateWrapper);
    }

    @Override
    @Cacheable(key = "#meterId + '.' + #lpTemplateId", unless = "#result == null")
    public List<CaptureObject> getCaptureObjs(Long meterId, Long lpTemplateId) {
        CaptureObjsStatus captureObjsStatus = lambdaQuery().eq(CaptureObjsStatus::getMeterId, meterId).eq(CaptureObjsStatus::getLpTemplateId, lpTemplateId)
                .eq(CaptureObjsStatus::getStatus, CaptureObjsStatus.Status.Success.getCode()).one();
        if (captureObjsStatus == null) {
            return null;
        }
        List<CaptureObject> result = CollUtil.newArrayList();
        List<String> list = JSONUtil.toList(JSONUtil.parseArray(captureObjsStatus.getResult()), String.class);
        for (String s : list) {
            String[] cs = s.split(":");
            CaptureObject captureObject = new CaptureObject();
            captureObject.setSubDataItemId(Convert.toLong(cs[0]));
            captureObject.setIndex(Convert.toInt(cs[1]));
            result.add(captureObject);
        }
        return result;
    }
}
